<template>
  <div class="app-container">
    <!-- 表格 -->
    <el-table
      :data="list"
      element-loading-text="数据加载中"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="title" label="名称" width="80"/>
      <el-table-column prop="imageUrl" label="图片地址" width="750"/>
      <el-table-column prop="linkUrl" label="链接地址" width="80"/>
      <el-table-column prop="sort" label="排序" width="80"/>
      <el-table-column prop="gmtCreate" label="创建时间" width="170"/>
      <el-table-column prop="gmtModified" label="更新时间" width="170"/>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/edu/banner/edit/' + scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit"
            >修改
            </el-button
            >
          </router-link>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeById(scope.row.id)"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center"
      layout="total, prev, pager, next, jumper"
      @current-change="getBannerByPage"
    />

  </div>
</template>

<script>
  // 引入调用
  import banner from '@/api/edu/banner'

  export default {
    // 写核心代码
    data() {
      return {
        list: null, // 查询接口返回值
        total: 0, // 总记录数
        page: 1, // 页码
        limit: 5, // 每页记录数
        teacherQuery: {} // 查询条件
      }
    },
    methods: {
      // 分页查询
      getBannerByPage(page = 1) {
        this.page = page
        banner.getPageList(this.page, this.limit)
          .then((response) => {
            // console.log(response)
            this.total = response.data.total
            this.list = response.data.items
          })
      },

      // 清空查询
      resetData() {
        // 表单输入项清空
        // this.teacherQuery = {}
        // 重新查询所有
        this.getBannerByPage()
      },

      // 删除
      removeById(id) {
        // 提示框
        this.$confirm('此操作将永久删除此记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 点击确定后删除
          banner.removeById(id)
            .then((response) => {
              if (response.code == 20000) {
                // 提示成功
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
                // 回到列表页面
                this.getBannerByPage()
              }
              // console.log(response);
            })
        })
      }
    },
    created() {
      this.getBannerByPage()
    }
  }
</script>
